#include "PointDbTest.h"
//CPPUNIT_TEST_SUITE_REGISTRATION (PointDbTest);

void PointDbTest::setUp(void) {
	// set up test environment (initializing objects)
	pointDb = new PointDb();
	paramDb = new ParameterDb();
	conDb = new ConstraintDb(pointDb,paramDb);
	primDb = new PrimitiveDb(pointDb,conDb);
	x= 10.0;
	y =20.0;
	pon = new Point(1, x, y);
	pon = pointDb->add(pon);

	pon2 = pointDb->add(new Point(2, y, x));

	pointDb->add(new Point(6, y, x));
	pon3 = pointDb->addPoint(2*x,2*y);

	sz=3;

	//testFunction6()
	Coordinate* cc1= new Coordinate(12.0,11.0);
	Coordinate* cc2= new Coordinate(22.0,12.0);

	Point* p1 = pointDb->addPoint(cc1);
	Point* p2 = pointDb->addPoint(cc2);
	Point* p3 = pointDb->addPoint(Coordinate(32.0,13.0));
	Point* p4 = pointDb->addPoint(Coordinate(42.0,14.0));
	paramDb->addParameter(0.5);
	paramDb->addParameter(0.6);
	paramDb->addParameter(0.7);

	Coordinate* lc1 = new Coordinate(89.0,98.0);
	Coordinate* lc2 = new Coordinate(189.0,198.0);
	primDb->addLine(lc1,lc2);
	primDb->addCircle(Coordinate(32.0,13.0),Coordinate(32.0,13.0));

	std::list<Coordinate*> punkty;
	punkty.push_back(cc1);
	punkty.push_back(cc2);
	punkty.push_back(new Coordinate(42.0,14.0));
	punkty.push_back(new Coordinate(42.0,14.0));

	primDb->addSegment(punkty);

	conDb->addConnectPoints(p1->getId(),p4->getId());
	conDb->addFixPoint(pointDb->get(10)->getId());
	conDb->addFixPoint(pointDb->get(11)->getId());
	conDb->addPerpendicular(p1->getId(),p2->getId(),p3->getId(),p4->getId());
	conDb->addHorizontal(p2->getId(),p4->getId());
}

void PointDbTest::tearDown(void) {
	//delete pon; pon=0;
	//delete pon2; pon2=NULL;
	//delete pon3; pon3=0;
	delete pointDb; pointDb=0;
	delete conDb;
	delete paramDb;
	delete primDb;
}

void PointDbTest::testFunction(void) {

	CPPUNIT_ASSERT(pointDb->isId(0));
	CPPUNIT_ASSERT(pointDb->isId(1));
	CPPUNIT_ASSERT(pointDb->isId(2));
	CPPUNIT_ASSERT(!pointDb->isId(3));
	CPPUNIT_ASSERT_EQUAL(pointDb->get(1),pon);
	CPPUNIT_ASSERT_EQUAL(pointDb->get(2),pon2);

	skreal fl = (*(pointDb->get(1)))*(*(pointDb->get(1)));
	CPPUNIT_ASSERT_EQUAL(x*x+y*y,fl);

	CPPUNIT_ASSERT_EQUAL(pon3->length(),pointDb->get(7)->length());
	CPPUNIT_ASSERT_EQUAL(pon3,pointDb->get(7));

	try {
		pointDb->remove(2);
	} catch (MyException& e) {
		e.print();
	}
	CPPUNIT_ASSERT_EQUAL(true,pointDb->isId(1));
	CPPUNIT_ASSERT_EQUAL(false,pointDb->isId(2));

	CPPUNIT_ASSERT_EQUAL(8,pointDb->getMaxId()+1);


}
void PointDbTest::testFunction2(void) {
	cout << endl << "*************************" << endl;
	std::cout << std::endl << pointDb->getMaxId()  << std::endl;

	std::cout << std::endl << "adr :" << pointDb->get(2) << " "
			<< (*pointDb->get(2)) << std::endl;

	std::cout << std::endl << "ad2r :" << pon2 << " " << *pon2 << std::endl;

	std::cout << std::endl << "adr :" << pointDb->get(1) << " "
			<< (*pointDb->get(1)) << std::endl;

	std::cout << std::endl << "ad2r :" << pon << " " << *pon << std::endl;

	_keyType* ks = pointDb->getIds();

	copy(ks->begin(), ks->end(), std::ostream_iterator<const int>(std::cout, " "));

	cout << endl << "*************************" << endl;

}

void PointDbTest::testFunction6() {
	cout << endl << "*************************" << endl;

	cout << "pointDb "<< endl << *pointDb ;
	cout << "conDb "<< endl<< *conDb ;
	cout << "paramDb "<< endl<< *paramDb ;
	cout << "primDb "<< endl<< *primDb ;

	cout << endl << "*************************" << endl;
}
